package com.centling.smartSealForPhone.activity;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.centling.smartSealForPhone.BuildConfig;
import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.bean.LoginBean;
import com.centling.smartSealForPhone.constants.GlobalData;
import com.centling.smartSealForPhone.constants.SPConstants;
import com.wsdk.service.BluetoothLeService;
import com.centling.smartSealForPhone.utils.BlueToothScanUtil;
import com.centling.smartSealForPhone.utils.ControlUtil;
import com.centling.smartSealForPhone.utils.ParseBluetoothData;
import com.centling.smartSealForPhone.utils.SPUtil;
import com.centling.smartSealForPhone.utils.ShowDialog;
import com.centling.smartSealForPhone.utils.ShowToast;
import com.google.gson.Gson;

public class SealChangeGuideOneActivity extends TitleBarActivity implements BlueToothScanUtil.StopSearchListener{
    private static final int OPEN_BLUE_DIALOG = 1000;

    private String macAddress;

    private BlueToothScanUtil blueToothScanUtil;

    private BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)){
                mProcessDialog.dismiss();
                if(BuildConfig.DEBUG){
                    ShowToast.shortTime("Seal Change Connected");
                }
                GlobalData.isConnect = true;
            } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
                byte[] data = intent.getByteArrayExtra(BluetoothLeService.EXTRA_DATA);
                if (ParseBluetoothData.isChangeStamp(data)) {
                    new ControlUtil().lock();
                } else if (ParseBluetoothData.parseLock(data)) {
                    ShowToast.shortTime("换章完成");
                    finish();
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seal_change_guide_one);

        setTitleBarText("换章");

        LoginBean loginBean = new Gson().fromJson(SPUtil.getString(SPConstants.LOGIN_JSON), LoginBean.class);
        macAddress = loginBean.getEquipRegisterInfo().getBlueToothMac();

        if(TextUtils.isEmpty(macAddress)){
            ShowToast.shortTime("获取用户绑定设备信息失败");
            finish();
        }

        TextView next = (TextView) findViewById(R.id.tv_guide_seal_change_btn1);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!BlueToothScanUtil.isBlueToothOpened()) {
                    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableBtIntent, OPEN_BLUE_DIALOG);
                    return;
                }
                if (GlobalData.isConnect) {
                    new ControlUtil().cancelChangeStamp();
                } else {
                    ShowDialog.showSelectDialog(mContext, "重新连接蓝牙", "蓝牙未连接，请先连接蓝牙", "", new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            mProcessDialog.setTitle("正在连接蓝牙").showNormal();
                            blueToothScanUtil.setMacAddress(macAddress);
                            blueToothScanUtil.startSearchBlueDevice();
                            blueToothScanUtil.setStopSearchListener(SealChangeGuideOneActivity.this);
                        }
                    });
                }
            }
        });

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
        intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
        registerReceiver(bluetoothReceiver, intentFilter);

        blueToothScanUtil = new BlueToothScanUtil(mContext);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!BlueToothScanUtil.isBlueToothOpened()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, OPEN_BLUE_DIALOG);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(bluetoothReceiver);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == OPEN_BLUE_DIALOG && resultCode == Activity.RESULT_CANCELED) {
            finish();
        }
    }

    @Override
    public void onStopped(boolean timeout) {
        mProcessDialog.dismiss();
        if(timeout) {
            ShowToast.shortTime("连接设备失败");
        }
    }
}
